package tt.dz.service.imp.sys;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.station.OmsStationExternalDao;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.station.OmsStationExternal;
import tt.dz.service.sys.StationExternalService;
import tt.dz.util.StringUtil;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class StationExternalServiceImpl implements StationExternalService{
	@Resource
	OmsStationExternalDao omsStationExternalDao;
	@SuppressWarnings("rawtypes")
	@Override
	public JsonForList getStationList(OmsStationExternal station) {
		return omsStationExternalDao.getStationList(station);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public JsonForList addStation(OmsStationExternal station) {
		return omsStationExternalDao.addStation(station);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public JsonForList updateStation(OmsStationExternal station) {
		return omsStationExternalDao.updateStation(station);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public JsonForList deleteStation(List<String> list) {
		return omsStationExternalDao.deleteStation(list);
	}

	@Override
	public  List<OmsStationExternal>  findAll() {
		List<OmsStationExternal> stationList = omsStationExternalDao.findAll(new OmsStationExternal());
		return stationList;
	}

	@Override
	public List<OmsStationExternal> getStationByIds(String stationIds) {
		String[]  list =  stationIds.split(",");
//		list.
		StringBuilder sb = new StringBuilder("where 1=1 ");
		if(list.length>0){
			sb.append("and station_id in (");
			for(String str :list){
				sb.append("'"+str+"',");
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append(")");
		}
		OmsStationExternal station = new OmsStationExternal();
		return omsStationExternalDao.findListByCondition(station, sb.toString());
	}

	@Override
	public List<OmsStationExternal> getStationByDistrictCode(String province_code, String city_code, String district_code) {
		OmsStationExternal OmsStationExternal = new OmsStationExternal();
		if(StringUtil.isBlank(district_code)){
			OmsStationExternal.setDistrict_code(district_code);
		}
		if(StringUtil.isBlank(city_code)){
			OmsStationExternal.setDistrict_code(city_code);
		}
		if(StringUtil.isBlank(province_code)){
			OmsStationExternal.setDistrict_code(province_code);
		}
		return omsStationExternalDao.findExample(OmsStationExternal, null, null, null, null);
	}

	@Override
	public String getNewStationId(OmsStationExternal station) {
		//暂时以前6位作区号，模糊查询目前存在的区号的站点。
		String district_code = station.getDistrict_code();//6位
//		String oper_merchant_id = station.getOper_merchant_id();//取运营商id后4位
		String stationId_query = district_code;
//		String sql = "select station_Id from oms_station where station_Id like '"+stationId_query+"%' order by station_Id desc ";
		OmsStationExternal omsStation_1 = omsStationExternalDao.findOneExampleByCondition(new OmsStationExternal(), "where station_Id like '"+stationId_query+"%'  and LENGTH(station_Id) = 20 order by station_Id desc ");
		if(omsStation_1 == null){
			return stationId_query +"0000"+ "0000000001";
		}
		String stationId_old = omsStation_1.getStation_id();
		String serial_str = stationId_old.substring(10, 20);
		return stationId_old.substring(0,10)+StringUtil.complement(String.valueOf((Integer.valueOf(serial_str)+1)), 10);
	}

	@Override
	public OmsStationExternal getStationById(String stationId) {
		OmsStationExternal station_query= new OmsStationExternal();
		station_query.setStation_id(stationId);
		return omsStationExternalDao.findOneExample(station_query, null, null);
	}

	@Override
	public List<OmsStationExternal> getStationByProvinceCode(String province_code) {
		OmsStationExternal station_query = new OmsStationExternal();
		
		StringBuffer conditionSql = new StringBuffer(" a ");
		conditionSql.append(" left join (select b.district_code dictrict_code_other, b.city_code,d.province_code from oms_dic_district_code b ,oms_dic_city_code c ,oms_dic_province_code d where b.city_code=c.city_code and c.province_code=d.province_code) dic_view ");
		conditionSql.append(" ON  dic_view.province_code='"+province_code+"'");
		conditionSql.append(" where dic_view.dictrict_code_other=a.district_code and a.valid_flag = 1");
		
		List<OmsStationExternal> lsStationExt = omsStationExternalDao.findListByConditionWithIgnore(station_query, conditionSql.toString());
		return lsStationExt;
	}
	

}
